<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="期数" prop="periods">
        <el-input
          v-model="queryParams.periods"
          placeholder="请输入期数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开奖日期" prop="kjrqDt">
        <el-date-picker clearable size="small"
                        v-model="queryParams.kjrqDt"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择开奖日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          plain-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--          v-hasPermi="['gamble:history:add']"-->
    <!--        >新增</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['gamble:history:edit']"-->
    <!--        >修改</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['gamble:history:remove']"-->
    <!--        >删除</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['gamble:history:export']"-->
    <!--        >导出</el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="historyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="收入id" align="center" prop="lottoHistoryId" v-if="false"/>
      <el-table-column label="期数" align="center" prop="periods" width="100"/>
      <el-table-column label="开奖日期" align="center" prop="kjrqDt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.kjrqDt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="位数1" align="center" prop="n1" width="80"/>
      <el-table-column label="位数2" align="center" prop="n2" width="80"/>
      <el-table-column label="位数3" align="center" prop="n3" width="80"/>
      <el-table-column label="位数4" align="center" prop="n4" width="80"/>
      <el-table-column label="位数5" align="center" prop="n5" width="80"/>
      <el-table-column label="位数6" align="center" prop="n6" width="80"/>
      <el-table-column label="位数7" align="center" prop="n7" width="80"/>
      <el-table-column label="顺序前数字" align="center">
        <template slot-scope="scope">
          <span>{{
              scope.row.n1 + " " + scope.row.n2 + " " + " " + scope.row.n3 + " " + scope.row.n4 + " " + scope.row.n5 + " " + scope.row.n6 + " " + scope.row.n7
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="顺序后数字" align="center" prop="number"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="false">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gamble:history:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gamble:history:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改历史大乐透对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="期数" prop="periods">
          <el-input v-model="form.periods" placeholder="请输入期数"/>
        </el-form-item>
        <el-form-item label="开奖日期" prop="kjrqDt">
          <el-date-picker clearable size="small"
                          v-model="form.kjrqDt"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择开奖日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="第一位数" prop="n1">
          <el-input v-model="form.n1" placeholder="请输入第一位数"/>
        </el-form-item>
        <el-form-item label="位数" prop="n2">
          <el-input v-model="form.n2" placeholder="请输入位数"/>
        </el-form-item>
        <el-form-item label="位数" prop="n3">
          <el-input v-model="form.n3" placeholder="请输入位数"/>
        </el-form-item>
        <el-form-item label="位数" prop="n4">
          <el-input v-model="form.n4" placeholder="请输入位数"/>
        </el-form-item>
        <el-form-item label="位数" prop="n5">
          <el-input v-model="form.n5" placeholder="请输入位数"/>
        </el-form-item>
        <el-form-item label="位数" prop="n6">
          <el-input v-model="form.n6" placeholder="请输入位数"/>
        </el-form-item>
        <el-form-item label="位数" prop="n7">
          <el-input v-model="form.n7" placeholder="请输入位数"/>
        </el-form-item>
        <el-form-item label="顺序后数字" prop="number">
          <el-input v-model="form.number" placeholder="请输入顺序后数字"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listHistory, getHistory, delHistory, addHistory, updateHistory} from "@/api/gamble/history";

export default {
  name: "History",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历史大乐透表格数据
      historyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        periods: null,
        kjrqDt: null,
        n1: null,
        n2: null,
        n3: null,
        n4: null,
        n5: null,
        n6: null,
        n7: null,
        number: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        periods: [
          {required: true, message: "期数不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询历史大乐透列表 */
    getList() {
      this.loading = true;
      listHistory(this.queryParams).then(response => {
        this.historyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lottoHistoryId: null,
        periods: null,
        kjrqDt: null,
        n1: null,
        n2: null,
        n3: null,
        n4: null,
        n5: null,
        n6: null,
        n7: null,
        number: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lottoHistoryId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加历史大乐透";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const lottoHistoryId = row.lottoHistoryId || this.ids
      getHistory(lottoHistoryId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改历史大乐透";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.lottoHistoryId != null) {
            updateHistory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHistory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const lottoHistoryIds = row.lottoHistoryId || this.ids;
      this.$modal.confirm('是否确认删除历史大乐透编号为"' + lottoHistoryIds + '"的数据项？').then(function () {
        return delHistory(lottoHistoryIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gamble/history/export', {
        ...this.queryParams
      }, `history_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
